package com.miqtech.master.wyrecorder.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.miqtech.master.wyrecorder.R;
import com.miqtech.master.wyrecorder.common.BaseRecycleViewAdapter;
import com.miqtech.master.wyrecorder.entity.LiveTypeInfo;
import com.miqtech.master.wyrecorder.entity.SearchWrapper;
import com.miqtech.master.wyrecorder.utils.DeviceUtils;
import com.miqtech.master.wyrecorder.utils.imageloader.AsyncImage;
import com.miqtech.master.wyrecorder.widget.roundImageView.RoundedImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/12/23.
 */

public class SelectGameAdatper extends BaseRecycleViewAdapter {
    public static final int VIEW_TITLE = 0;
    public static final int VIEW_ITEM = 1;
    public static final int VIEW_HISTORY_ITEM = 2;
    private List<SearchWrapper<Object>> datas;

    public SelectGameAdatper(Context context, List<SearchWrapper<Object>> datas) {
        super(context);
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case VIEW_TITLE:
                convertView = mInflater.inflate(R.layout.layout_select_game_tag, parent, false);
                holder = new TagHolder(convertView);
                break;
            case VIEW_HISTORY_ITEM:
            case VIEW_ITEM:
                convertView = mInflater.inflate(R.layout.layout_select_game_item, parent, false);
                holder = new GameHolder(convertView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TagHolder) {
            setTag((TagHolder) holder, position);
        } else if (holder instanceof GameHolder) {
            setGameItem((GameHolder) holder, position);
        }

    }

    private void setGameItem(GameHolder holder, final int position) {
        if (datas != null) {
            LiveTypeInfo liveTypeInfo = ((LiveTypeInfo) datas.get(position).data);
            if (liveTypeInfo != null) {
                AsyncImage.displayImage(liveTypeInfo.getIcon(), holder.ivGamePic);
                holder.tvGameName.setText(liveTypeInfo.getName());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClickListener.onItemClick(v, position);
                    }
                });
                LinearLayout.LayoutParams rlParams = (LinearLayout.LayoutParams) holder.llItem.getLayoutParams();
                if (liveTypeInfo.getPosition() % 2 == 0) {
                    rlParams.setMargins(DeviceUtils.dp2px(mContext, 12), 0, DeviceUtils.dp2px(mContext, 6), 0);
                } else {
                    rlParams.setMargins(DeviceUtils.dp2px(mContext, 6), 0, DeviceUtils.dp2px(mContext, 12), 0);
                }
            }
        }
    }

    private void setTag(TagHolder holder, int position) {
        holder.viewLeft.setVisibility(View.VISIBLE);
        holder.tvTagMore.setVisibility(View.GONE);
        if (datas.get(position).data.toString().equals("最近选择")) {
            holder.tvTagName.setText("最近选择");
        } else if (datas.get(position).data.toString().equals("全部游戏")) {
            holder.tvTagName.setText("全部游戏");
        }
    }

    @Override
    public int getItemCount() {
        if (datas != null && !datas.isEmpty()) {
            return datas.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return datas.get(position).type;
    }

    static class TagHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.view_left)
        View viewLeft;
        @BindView(R.id.tv_tag_name)
        TextView tvTagName;
        @BindView(R.id.tv_tag_more)
        TextView tvTagMore;
        @BindView(R.id.llSearchTag)
        LinearLayout llSearchTag;

        public TagHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class GameHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.llItem)
        LinearLayout llItem;
        @BindView(R.id.ivGamePic)
        RoundedImageView ivGamePic;
        @BindView(R.id.tvGameName)
        TextView tvGameName;

        public GameHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

}
